<?php

    class gcanje 
    {
        public $sql;
        
        function gcanje() 
        {
            $this->sql = new DmpSql();
        }
        
        function nuevo_canje ( $fecha, $idpersona, $idusuario="" ) 
        {
            $consulta="INSERT INTO canje 
                        SET fecha='".$fecha."', 
                        idpersona='".$idpersona."', 
                        idusuario='".$idusuario."'";
            
            $error = 0;
            $this->sql->consultar("START TRANSACTION","sigh");
            
            if (!$this->sql->consultar($consulta, "sigh")) 
            {
                $error++;
            }
            
            if($error == 0)
            {
                $this->sql->consultar("COMMIT","sigh");
                return mysql_insert_id();
            }
            else
            {
                $this->sql->consultar("ROLLBACK","sigh");
                return 0;
            }		
        }
        
        function nuevo_canje_letra ( $idcanje, $idcp_egreso ) 
        {
            $consulta="INSERT INTO canje_letra 
                        SET idcanje='".$idcanje."', 
                        idcp_egreso='".$idcp_egreso."'";
            
            $error = 0;
            $this->sql->consultar("START TRANSACTION","sigh");
            
            if (!$this->sql->consultar($consulta, "sigh")) 
            {
                $error++;
            }
            
            if($error == 0)
            {
                $this->sql->consultar("COMMIT","sigh");
                return true;
            }
            else
            {
                $this->sql->consultar("ROLLBACK","sigh");
                return false;
            }
        }
        
        function nuevo_canje_cp_egreso ( $idcanje, $idcp_egreso ) 
        {
            $consulta="INSERT INTO canje_cp_egreso 
                        SET idcanje='".$idcanje."', 
                        idcp_egreso='".$idcp_egreso."'";
            
            $error = 0;
            $this->sql->consultar("START TRANSACTION","sigh");
            
            if (!$this->sql->consultar($consulta, "sigh")) 
            {
                $error++;
            }
            
            if($error == 0)
            {
                $this->sql->consultar("COMMIT","sigh");
                return true;
            }
            else
            {
                $this->sql->consultar("ROLLBACK","sigh");
                return false;
            }
        }
        
        function editar_canje ( $idcanje, $fecha, $idpersona, $idusuario ) 
        {
            $consulta = "UPDATE canje
                         SET fecha='".$fecha."',
                         idpersona='".$idpersona."',
                         idusuario='".$idusuario."'
                         WHERE idcanje=.'".$idcanje."'";  
                         
            $error = 0;
            $this->sql->consultar("START TRANSACTION","sigh");
            
            if (!$this->sql->consultar($consulta, "sigh")) 
            {
                $error++;
            }
            
            if($error == 0)
            {
                $this->sql->consultar("COMMIT","sigh");
                return true;
            }
            else
            {
                $this->sql->consultar("ROLLBACK","sigh");
                return false;
            }
        }
    }

?>